<?php

class Encrypter {

    private static $enc_key = "QrWcxVAYYYVS56xjsTpJHVJS";
    private static $iv = "fYfhHeDm";
    private static $bit_check = 8;

    private static function getKey() {
        return $this->enc_key;
    }

	public function __construct($key=NULL, $iv=NULL, $bit_check=8){
		if( $key )
			self::$enc_key = $key;
		if( $iv )
			self::$iv = $iv;
		if( $bit_check !== self::$bit_check )
			self::$bit_check = $bit_check;
	}
	
    public static function encrypt($input) {
        if (empty($input))
            throw new Exception("Cannot encrypt empty string.");
        $text_num = str_split($input, self::$bit_check);
        $text_num = $bit_check - strlen($text_num[count($text_num) - 1]);
        for ($i = 0; $i < $text_num; $i++) {
            $text = $text . chr($text_num);
        }
        $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
        mcrypt_generic_init($cipher, self::$enc_key, self::$iv);
        $decrypted = mcrypt_generic($cipher, $input);
        mcrypt_generic_deinit($cipher);
        return base64_encode($decrypted);
    }

    public static function decrypt($input) {
        if (empty($input))
            throw new Exception("Cannot decrypt empty string.");

        $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
        mcrypt_generic_init($cipher, self::$enc_key, self::$iv);
        $decrypted = mdecrypt_generic($cipher, base64_decode($input));
        mcrypt_generic_deinit($cipher);
        $last_char = substr($decrypted, -1);
        for ($i = 0; $i < self::$bit_check - 1; $i++) {
            if (chr($i) == $last_char) {
                $decrypted = substr($decrypted, 0, strlen($decrypted) - $i);
                break;
            }
        }
        return $decrypted;
    }

}

?>
